package com.freshcity.page.home
{
	import com.freshcity.component.FLabel;
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.container.HBox;
	import com.freshcity.component.container.VBox;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.graphics.LevelUpWin;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.lib.util.ObjectPool;
	import com.freshcity.manager.CommunicatorManager;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.support.MaskInfo;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	
	import fl.controls.ComboBox;
	import fl.controls.Label;
	import fl.controls.RadioButtonGroup;
	import fl.controls.TextInput;
	import fl.data.DataProvider;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class UpgradeLevel extends LevelUpWin
	{
		private var main:MainScene;
		private var user:*;

		private var propertyItem1:*;
		private var propertyItem2:*;
		private var propertyItem3:*;
		
		private var provinceBox:ComboBox;
		private var xml:XML=LoaderMax.getContent("dataXML");
		private var mainContent:Sprite;
		private var swfLoader:SWFLoader;

		public function UpgradeLevel(main:*)
		{
			super();
			this.main = main;
			this.user=main.user;
			init();
		}

		private function init():void
		{
			this.content.text="下一级需要提供如下信息：";

			provinceBox=new ComboBox();
			var da:Array=[];
			da.push({label: '请选择'});
			for each (var x:XML in xml.cities.prov)
			{
				da.push({label: x.@label});
			}
			provinceBox.dataProvider=new DataProvider(da);
			provinceBox.addEventListener(Event.CHANGE, provinceSelected);

			mainContent=new Sprite();
			mainContent.name="mainContent";
			mainContent.x=this.content.x + 5;
			mainContent.y=this.content.y + 25;
			mainContent.mouseEnabled=false;
			mainContent.tabEnabled=false;
			setInfoItem(mainContent);
			this.addChild(mainContent);

			var loc1:*=ObjectPool.getObject(MaskInfo);
			EffectManager.addUIMask([loc1], 0);

			this.closeBtn.addEventListener(MouseEvent.CLICK, onCloseBtnHandler);
			this.confirmBtn.addEventListener(MouseEvent.CLICK, onConfirmBtnHandler);
		}

		protected function onCloseBtnHandler(event:MouseEvent):void
		{
			this.closeBtn.removeEventListener(MouseEvent.CLICK, onCloseBtnHandler);
			this.parent.removeChild(this);
			EffectManager.hideUIMask();
		}

		protected function onConfirmBtnHandler(event:MouseEvent):void
		{
			if (propertyItem1 is Label && propertyItem1.name == "avatarPic")
			{
				loadPictureEditor();
				return;
			}
			
			var value1:* = this.getPropertyValue(this.propertyItem1);
			var value2:* = this.getPropertyValue(this.propertyItem2);
			var value3:* = this.getPropertyValue(this.propertyItem3);

			if (value1!=null && value2!=null && value3!=null)
			{
				submitDialog();
			}else{
				main.msg.showRollTipsMsg("提交数据不完整！");
			}
		}
		
		private function submitDialog():void
		{
			var htmlContent:String="个人信息一旦提交"+StringUtil.addColor("不可随意修改","#ff0000")+"，确认继续吗？";
			var htmlTitle:String="<font color=\'#f5ff00\'>升级</font>";
			
			var value1:* = this.getPropertyValue(this.propertyItem1);
			var value2:* = this.getPropertyValue(this.propertyItem2);
			var value3:* = this.getPropertyValue(this.propertyItem3);
			
			var _confirm:Function=function(arg1:int):void
			{
				if (arg1 == Alert.OK)
				{ 
				}
			};
			//Alert.show(htmlContent, htmlTitle, Alert.OK | Alert.CANCEL, null, _confirm);
			main.commu.send(OpCodes.UPGRADE_LEVEL, {level:String(main.user.level), data1: value1,data2:value2,data3:value3});
		}
		
		private function getPropertyValue(propertyItem:*):*
		{
			var value:*;
			if(propertyItem==undefined) value = "";
			if (propertyItem is ComboBox && propertyItem.selectedIndex > 0)
			{
				if (propertyItem.name=='address')
				{
					value=provinceBox.selectedLabel + '-' +propertyItem.selectedLabel;
				}
				else
				{
					value=propertyItem.selectedLabel;
				}
			}
			else if (propertyItem is TextInput && propertyItem.text != "")
			{
				value=propertyItem.text;
			}
			else if (propertyItem is RadioButtonGroup)
			{
				value=propertyItem.selection.value;
			}			
			return value;
		}

		private function loadPictureEditor():void
		{
			//create a SWFLoader that will add the content to the display list at position x:50, y:100 when it has loaded:
			swfLoader=new SWFLoader(GlobalVar.MAIN_PATH+"/CutView.swf", {name: "mainSWF", container: this.parent, x: 50, y: 50, onInit: initHandler, estimatedBytes: 9500});
			//begin loading
			swfLoader.load();
		}

		private function initHandler(event:LoaderEvent):void
		{
			//fade the swf in as soon as it inits
			this.parent.removeChild(this);
			var sp:DisplayObject=event.target.content as DisplayObject;
			TweenLite.from(sp, 1, {alpha: 0});
			//get a MovieClip named "phoneAnimation_mc" that's on the root of the subloaded swf
			//var mc:DisplayObject = loader.getSWFChild("phoneAnimation_mc");
			//find the "com.greensock.TweenLite" class that's inside the subloaded swf
			var tweenClass:Class=swfLoader.getClass("com.freshcity.upload.CutView");
			main.stage.addEventListener(EventName.CancelPicEdit, cancelPictureEditor);
			main.stage.addEventListener(EventName.SavePic, savePicture);
		}

		protected function savePicture(event:Event):void
		{
			var evt:*=event as Object;
			main.commu.send(OpCodes.UPLOAD_PIC, evt.data.content, savaPictureCallBack);
		}

		private function savaPictureCallBack(data:*):void
		{
			if (data == true)
			{
				main.commu.send(OpCodes.UPGRADE_LEVEL, {level:String(main.user.level), data: ""});
			}
			else
			{
				main.msg.showRollTipsMsg("抱歉，数据更新失败。");
			}
			swfLoader.dispose(true);
			EffectManager.hideUIMask();
		}

		protected function cancelPictureEditor(event:Event):void
		{
			main.stage.removeEventListener(EventName.CancelPicEdit, cancelPictureEditor);
			main.stage.removeEventListener(EventName.SavePic, savePicture);
			swfLoader.dispose(true);
			EffectManager.hideUIMask();
		}

		public function levelUpFail(user:*):void
		{
			this.content.text="升级失败：升级条件不足";
			while (mainContent.getChildAt(0))
			{
				mainContent.removeChildAt(0);
			}
			var l1:Label=new Label();
			l1.width=300;
			l1.htmlText="升级需要美誉度达到"+StringUtil.addColor(user.needIncome, "0xff0000");
			mainContent.addChild(l1);

			var l2:Label=new Label();
			l2.width=300;
			l2.htmlText="当前美誉度"+StringUtil.addColor(user.income, "0x999999");
			l2.y=40;
			mainContent.addChild(l2);
			this.confirmBtn.visible=false;
		}

		private function provinceSelected(e:Event):void
		{
			var da:Array=[];
			var c:ComboBox=e.currentTarget as ComboBox;
			da.push({label: '请选择'});
			for each (var x:XML in xml.cities.prov.(@label == c.selectedItem.label).children())
			{
				da.push({label: x.@value});
			}
			propertyItem1.dataProvider=new DataProvider(da);
		}
		
		private function initCombox(container:DisplayObjectContainer,desc:String,dataSrc:XMLList,w:Number=100):Sprite
		{
			var item2:HBox = new HBox();
			var lb2:Label=new Label();
			lb2.text=desc;
			lb2.width=60;
			
			var dataArray:Array=[];
			var x:XML;
			var cb:ComboBox=new ComboBox();
			cb.width=w;
			dataArray.push({label: '请选择'});
			for each (x in dataSrc)
			{
				dataArray.push({label: x.@value});
			}
			cb.dataProvider=new DataProvider(dataArray);
			item2.addChild(lb2);
			item2.addChild(cb);
			item2.initialize();
			container.addChild(item2);
			return cb;
		}
		
		private function initInput(container:DisplayObjectContainer,desc:String,remark:String=null,w:Number=40):Sprite
		{
			var item2:HBox = new HBox();
			var lb2:Label=new Label();
			lb2.text=desc;
			lb2.width=60;
			
			var cb:TextInput=new TextInput();
			cb.width = w;
			
			item2.addChild(lb2);
			item2.addChild(cb);
			
			if(remark){
				var lb3:Label=new Label();
				lb3.text=remark;
				lb3.width=60;
			 	item2.addChild(lb3);
			}
			item2.initialize();
			container.addChild(item2);
			return cb;
		}
		
		//1级升级信息设置
		private function levelOne():Sprite
		{
			var sp:VBox = new VBox();
			var item1:HBox = new HBox();
			var lb:Label = new Label();
			lb.text="籍贯";
			lb.width=60;
			
			propertyItem1=new ComboBox();
			propertyItem1.name="address";
			item1.addChild(lb);
			item1.addChild(provinceBox);
			item1.addChild(propertyItem1);
			item1.initialize();
			sp.addChild(item1);
			 
			this.propertyItem2 =this.initCombox(sp,"星座",xml.xingzuo.children());
			this.propertyItem3 =this.initCombox(sp,"属相",xml.shengxiao.children());
			sp.initialize();
			return sp;
		}
		
		//二级信息设置
		private function levelTwo():Sprite
		{
			var sp:VBox = new VBox();
			this.propertyItem1 =this.initCombox(sp,"职业",xml.hangye.children(),180);
			this.propertyItem2 =this.initCombox(sp,"公司类型",xml.leixing.children());
			this.propertyItem3 =this.initInput(sp,"体重","公斤");
			sp.initialize();
			return sp;
		}
		
		//三级信息设置
		private function levelThree():Sprite
		{
			//上传照片
			var sp:HBox = new HBox();
			propertyItem1=new Label();
			propertyItem1.name="avatarPic";
			propertyItem1.text="提供个人头像照片";
			sp.addChild(propertyItem1);
			sp.initialize();
			return sp;
		}
		
		//四级信息设置
		private function levelFour():Sprite
		{
			var sp:VBox = new VBox();
			this.propertyItem1 =this.initCombox(sp,"购车",xml.gouche.children());
			this.propertyItem2 =this.initCombox(sp,"住房",xml.goufang.children());
			this.propertyItem3 =this.initCombox(sp,"年收入",xml.nianxin.children());
			sp.initialize();
			return sp;
		}
		
		private function levelFive():Sprite
		{
			var sp:VBox = new VBox();
			this.propertyItem1 =this.initCombox(sp,"吸烟",xml.xiyan.children());
			this.propertyItem2 =this.initCombox(sp,"饮酒",xml.yinjiu.children());
			this.propertyItem3 =this.initCombox(sp,"作息习惯",xml.zuoxi.children());
			sp.initialize();
			return sp;
		}
		
		private function setInfoItem(arg1:Sprite):void
		{
			var lb:Label;
			var level:int=user.level;

			var sp:*;
			//sp.alignment=HBox.MIDDLE;

			/*
			var funLb:Label=new Label();
			funLb.name="funLb";
			funLb.width=350;
			funLb.y=70;
			funLb.text="*较低级别正限制你使用高级功能。";

			var line:TimelineMax=new TimelineMax();
			line.repeat=-1;
			line.repeatDelay=1;
			line.append(new TweenLite(funLb, 0.5, {alpha: 0}));
			line.append(new TweenLite(funLb, 0.5, {alpha: 1}));
			*/

			var tipLb:TextField=new TextField();
			tipLb.name="tipLb";
			tipLb.height=40;
			tipLb.width=320;
			tipLb.y=90;
			tipLb.wordWrap=true;
			tipLb.multiline=true;
			var str:String = "注:个人信息平等互换，每人只能按自身信用等级查看他人信息。";
			//str +=	(main.user.sex=='female'?"可过滤低信用的人，提高交友安全度。":"表明交友诚意，方便认识更多的人。");
			tipLb.htmlText=StringUtil.addColor(str, "#ff0000");


			switch (level)
			{
				case 1:
				{
					sp = this.levelOne();
					break;
				}
				case 2:
				{
					sp = this.levelTwo();
					//funLb.htmlText="*".concat("新级别可访问", StringUtil.addBold("城市广场", "#000000"), "，和同城的人聊天。");
					break;
				}
				case 3:
				{
					sp = this.levelThree();
					//funLb.htmlText="*".concat("新级别可撰写", StringUtil.addBold("聊天备忘", "#FF0000"), "");
					break;
				}
				case 4:
				{
					sp = this.levelFour();
					//funLb.htmlText="*".concat("新级别可查看", StringUtil.addBold("聊天印象", "#FF0000"), "");
					break;
				}
				case 5:
				{
					sp = this.levelFive();
					break;
				}
				default:
				{
					break;
				}
			}

			arg1.addChild(sp);
			//arg1.addChild(funLb);
			arg1.addChild(tipLb);
		}
	}
}